﻿using FunctionLib.Help;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FunctionLib.Single
{
    /// <summary>
    /// params:
    /// 0:sourceId obj
    /// 1:quantityId obj
    /// 2:year num
    /// 3:month num
    /// 4:day num
    /// 5:calcType num
    /// 6:timeRange1 start num
    /// 7:timeRange1 end num
    /// 8:timeRange2 start num
    /// 9:timeRange2 end num
    /// 10:timeRange3 start num
    /// 11:timeRange3 end num
    /// 12:timeRange4 start num
    /// 13:timeRange4 end num
    /// {equipID}, {pointID},{textFieldYear.Text},{textFieldMonth.Text},{day},{calctype},{timeRangeString}
    /// </summary>
    public class PMEGetDayData : IFunction
    {
        public string Name => "PMEGetDayData";

        public int MinParam => 6;

        public int MaxParam => 14;

        public object Calc(params object[] param)
		{
			object result;
			try
			{
				if (param.Length < 6)
				{
					result = "参数数量错误";
				}
				else if (param[0] == null || param[1] == null)
				{
					result = "参数值1、2为空";
				}
				else
				{
					for (int i = 3; i <= 6; i++)
					{
						string checkRes = QueryHelp.NumCheck(param[i - 1]);
						if (checkRes != null)
						{
							return checkRes + ":" + i.ToString();
						}
					}
					object sourceId = param[0];
					object quantityId = param[1];
					DateTime tempDate;
					if (!DateTime.TryParse(string.Concat(new string[]
					{
						param[2].ToString(),
						"-",
						param[3].ToString(),
						"-",
						param[4].ToString()
					}), out tempDate))
					{
						result = "年月日字段错误";
					}
					else
					{
						int calcTypeInt = -1;
						string calcType = QueryHelp.GetCalcType(param[5], ref calcTypeInt);
						double resNumber = 0.0;
						int avgCount = 0;
						DateTime resTime = default(DateTime);
						if (param.Length == 6)
						{
							DateTime start = tempDate;
							DateTime end = tempDate.AddHours(23.0);
							object res = FunctionHelp.GetTimeRangeResult(sourceId, quantityId, start, end, calcType, calcTypeInt, ref resTime);
							result = FunctionHelp.GetFunctionReturnResult(calcTypeInt, resTime, (res.GetType() != typeof(double) || double.IsNaN(Convert.ToDouble(res))) ? 0.0 : Convert.ToDouble(res));
						}
						else
						{
							for (int j = 0; j < 4; j++)
							{
								if (param.Length >= 8 + j * 2)
								{
									object addRes = FunctionHelp.ResAdd(7 + j * 2, param[6 + j * 2], param[7 + j * 2], sourceId, quantityId, tempDate, calcType, ref resNumber, calcTypeInt, ref avgCount, ref resTime);
									if (addRes != null)
									{
										return addRes;
									}
								}
							}
							if (calcTypeInt == 2)
							{
								if (avgCount == 0 || resNumber == 0.0)
								{
									resNumber = 0.0;
								}
								else
								{
									resNumber /= (double)avgCount;
								}
							}
							result = FunctionHelp.GetFunctionReturnResult(calcTypeInt, resTime, resNumber);
						}
					}
				}
			}
			catch (Exception ex)
			{
				result = ex.Message;
			}
			return result;
		}
    }
}
